const path = require('path')

function resolve (dir) {
  return path.join(__dirname, dir)
}

const Timestamp = new Date().getTime()

const webpack = require('webpack')
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
const gitRevisionPlugin = new GitRevisionPlugin()
const gitRevision = {
  VERSION: gitRevisionPlugin.version(),
  COMMITHASH: gitRevisionPlugin.commithash(),
  BRANCH: gitRevisionPlugin.branch(),
  LASTCOMMITDATETIME: gitRevisionPlugin.lastcommitdatetime()
}

const px2rem = require('postcss-px2rem');
// 配置基本大小
const postcss = px2rem({
  // 基准大小 baseSize，需要和rem.js中相同
  remUnit: 16
})

module.exports = {
  runtimeCompiler: true,
  lintOnSave: true,
  publicPath: '/',
  // publicPath: '/v2/',
  // 构建时输出的目录
  outputDir: 'dist',
  assetsDir: 'static',
  indexPath: 'index.html',
  filenameHashing: false,
  // 生产环境是否生成 sourceMap 文件
  productionSourceMap: false,
  devServer: {
    proxy: {
      //设置代理
      '/api': {
        target: process.env.VUE_APP_URL,
        changeOrigin: true,
        pathRewrite: { '^/api': '' }
      },
    }
  },
  css: {
    // sourceMap: true,
    extract: process.env.NODE_ENV === 'production' ? {
      ignoreOrder: true,
    } : false,
    // extract: false,
    loaderOptions: {
      postcss: {
        plugins: [
          postcss
        ],
      }
    },
  },
  chainWebpack: config => {
    config.plugin('html').tap(args => {
      args[0].title = '物流源';
      return args;
    })

    if (process.env.NODE_ENV === 'production') {
      // // 给js和css 配置版本号
      config.output.filename('js/[name].' + Timestamp + '.js').end();
      config.output.chunkFilename('js/[name].' + Timestamp + '.js').end();
      config.plugin('extract-css').tap(args => [{
        filename: `css/[name].${Timestamp}.css`,
        chunkFilename: `css/[name].${Timestamp}.css`
      }])

      // 指定 iconfont 打包路径
      config.module
        .rule('fonts')
        .use('url-loader')
        .loader('url-loader')
        .tap(options => {
          options['limit'] = 4096
          options['publicPath'] = '/'
          return options;
        })
    }

    // 别名设置
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@api', resolve('src/api'))
      .set('@components', resolve('src/components'))
      .set('@views', resolve('src/views'))
      .set('@config', resolve('src/config'))
      .set('@static', resolve('src/static'))
      .set('@store', resolve('src/store'))
      .set('@packages', resolve('packages'))
      .set('@menu', resolve('packages/menu'))
      .set('@basic', resolve('packages/basic'))
      .set('@tablra', resolve('packages/tablra'))
      .set('@formla', resolve('packages/formla'))
      .set('@service', resolve('packages/service'))
      .set('@tagsView', resolve('packages/tagsView'))
  },
  configureWebpack: config => {
    return {
      plugins: [
        // 打印版本号
        new webpack.DefinePlugin({
          'process.env.gitRevision': JSON.stringify(gitRevision)
        })
      ]
    }
  },
}
